package com.snoopy.gas1.service.impl;

import com.snoopy.gas1.entity.Oil_info;
import com.snoopy.gas1.mapper.Oil_infoMapper;
import com.snoopy.gas1.service.Oil_infoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Oil_infoServiceImpl implements Oil_infoService {

    @Autowired
    private Oil_infoMapper oilInfoMapper;

    @Override
    public List<Oil_info> lists() {
        return oilInfoMapper.lists();
    }

    @Override
    public List<Oil_info> searchByName(String name) {
        if (name == null || name.trim().isEmpty()) {
            return lists(); // 空查询时返回所有
        }
        return oilInfoMapper.searchByName(name);
    }

    @Override
    public void deleteById(int id) {
        if (id > 0) {
            oilInfoMapper.deleteById(id);
        }
    }

    @Override
    public Object getAllOils() {
        return null;
    }

    @Override
    public boolean existsById(int oilId) {
        return false;
    }

    @Override
    public int add(Oil_info oilInfo) {
        // 简单校验：油品名称和价格不为空
        if (oilInfo.getOilName() == null || oilInfo.getOilName().trim().isEmpty()) {
            throw new RuntimeException("油品名称不能为空");
        }
        if (oilInfo.getUnitPrice() == null || oilInfo.getUnitPrice().doubleValue() <= 0) {
            throw new RuntimeException("单价必须大于0");
        }
        return oilInfoMapper.add(oilInfo);
    }

    @Override
    public Oil_info getById(int id) {
        return oilInfoMapper.getById(id);
    }

    @Override
    public int update(Oil_info oilInfo) {
        // 校验逻辑同添加
        if (oilInfo.getOilName() == null || oilInfo.getOilName().trim().isEmpty()) {
            throw new RuntimeException("油品名称不能为空");
        }
        if (oilInfo.getUnitPrice() == null || oilInfo.getUnitPrice().doubleValue() <= 0) {
            throw new RuntimeException("单价必须大于0");
        }
        return oilInfoMapper.update(oilInfo);
    }
}